/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import dao.ServiceManager;
import java.util.Vector;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import listener.IDownloadListener;
import model.ErrorMessage;
import model.Help;
import ui.Detail;
import ui.TextItem;

/**
 *
 * @author tho.hoang
 */
public class HelpDetailForm extends AForm implements ICommandListener {

    private Detail detail;
    private Vector helpDetail;

    public HelpDetailForm() {
    }

    public HelpDetailForm(MStockMidlet control, Display display, int branch, int option) {
        super(control, display);
        this.branch = branch;
        this.option = option;
    }

    protected void initData() {
        lang = new String[]{"Hướng dẫn", "Trở về"};

        recycle = control.getRecycle();
        this.icon = control.getIcons()[8];
    }

    protected void initGui() {
        if (recycle.getHelpTitle() != null && recycle.getHelpTitle().length() > 0) {
            detail = new Detail(display, control, recycle.getHelpTitle(), background, icon);
        } else {
            detail = new Detail(display, control, lang[0], background, icon);
        }

        back = new Command(lang[1], Command.BACK);

        detail.setCancelCommand(back);
        detail.setCommandListener(this);

        loadData();
    }

    protected void loadData() {
        if (option == State.NONE) {
            ServiceManager.getHelpDetail(this.control, 1, recycle.getHelpID(), new IDownloadListener() {

                public void completedAction(Object result) {
                    if (result != null) {
                        if (result instanceof ErrorMessage) {
                            showError((ErrorMessage) result);
                        } else {
                            Vector temp = (Vector) result;
                            if (temp.size() > 0) {
                                helpDetail = temp;
                                showData();
                            } else {
                                showError(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                            }
                        }
                    } else {
                        // ket qua tra ve null
                        showError(new ErrorMessage(ErrorMessage.ERROR_TRANSFER_DATA));
                    }
                }
            });

        } else {
            ServiceManager.getAboutUs(this.control, 1, MStockMidlet.PROVIDER, new IDownloadListener() {

                public void completedAction(Object result) {
                    if (result != null) {
                        if (result instanceof ErrorMessage) {
                            showError((ErrorMessage) result);
                        } else {
                            Vector temp = (Vector) result;
                            if (temp.size() > 0) {
                                helpDetail = temp;
                                showData();
                            } else {
                                showError(new ErrorMessage(ErrorMessage.ERROR_UPDATE_DATA));
                            }
                        }
                    } else {
                        // ket qua tra ve null
                        showError(new ErrorMessage(ErrorMessage.ERROR_TRANSFER_DATA));
                    }
                }
            });
        }
    }

    protected void showData() {
        Help help;
        for (int i = 0; i < helpDetail.size(); i++) {
            help = (Help) helpDetail.elementAt(i);
            detail.addItem(new TextItem(String.valueOf(help.getId()), help.getName(), help.getContent()));
        }

        detail.show();
    }

    protected void showError(ErrorMessage error) {
        detail.setSelectCommand(null);
        detail.addItem(new TextItem("-1", "", error.getMessage()));
        detail.show();
    }

    public void commandAction(Command command) {
        if (command == back) {
            control.back(0);
        }
    }
}
